package com.huilian.cloud.module.system.controller.admin.frontendpermission;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.CommonResult;
import com.huilian.cloud.framework.common.util.object.BeanUtils;
import static com.huilian.cloud.framework.common.pojo.CommonResult.success;

import com.huilian.cloud.framework.excel.core.util.ExcelUtils;

import com.huilian.cloud.framework.apilog.core.annotation.ApiAccessLog;
import static com.huilian.cloud.framework.apilog.core.enums.OperateTypeEnum.*;

import com.huilian.cloud.module.system.controller.admin.frontendpermission.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.frontendpermission.FrontendPermissionDO;
import com.huilian.cloud.module.system.service.frontendpermission.FrontendPermissionService;

@Tag(name = "管理后台 - 前端权限")
@RestController
@RequestMapping("/sys/frontend-permission")
@Validated
public class FrontendPermissionController {

    @Resource
    private FrontendPermissionService frontendPermissionService;

    @PostMapping("/create")
    @Operation(summary = "创建前端权限")
    @PreAuthorize("@ss.hasPermission('sys:frontend-permission:create')")
    public CommonResult<String> createFrontendPermission(@Valid @RequestBody FrontendPermissionSaveReqVO createReqVO) {
        return success(frontendPermissionService.createFrontendPermission(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新前端权限")
    @PreAuthorize("@ss.hasPermission('sys:frontend-permission:update')")
    public CommonResult<Boolean> updateFrontendPermission(@Valid @RequestBody FrontendPermissionSaveReqVO updateReqVO) {
        frontendPermissionService.updateFrontendPermission(updateReqVO);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得前端权限")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:frontend-permission:query')")
    public CommonResult<FrontendPermissionRespVO> getFrontendPermission(@RequestParam("id") String id) {
        FrontendPermissionDO frontendPermission = frontendPermissionService.getFrontendPermission(id);
        return success(BeanUtils.toBean(frontendPermission, FrontendPermissionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得前端权限分页")
    @PreAuthorize("@ss.hasPermission('sys:frontend-permission:query')")
    public CommonResult<PageResult<FrontendPermissionRespVO>> getFrontendPermissionPage(@Valid FrontendPermissionPageReqVO pageReqVO) {
        PageResult<FrontendPermissionDO> pageResult = frontendPermissionService.getFrontendPermissionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FrontendPermissionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出前端权限 Excel")
    @PreAuthorize("@ss.hasPermission('sys:frontend-permission:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFrontendPermissionExcel(@Valid FrontendPermissionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FrontendPermissionDO> list = frontendPermissionService.getFrontendPermissionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "前端权限.xls", "数据", FrontendPermissionRespVO.class,
                        BeanUtils.toBean(list, FrontendPermissionRespVO.class));
    }

}